import React from "react";
import { login } from "../util/request/api";
class Home extends React.Component {
  constructor() {
    super();
    this.state = {
      loginForm: {
        phone: "",
        password: "",
      },
    };
  }
  //获取登录表单的事件
  changeLogin(type, e) {
    this.setState({
      loginForm: {
        ...this.state.loginForm,
        [type]: e.target.value,
      },
    });
  }
  //封装一个登录事件
  login() {
    login(this.state.loginForm).then((res) => {
      console.log(res, "登录结果");
      if (res.code == 200) {
        //进行存储
        sessionStorage.setItem("isLogin", JSON.stringify(res.list));
        //跳转到首页
        this.props.history.push("/index/home");
        alert(res.msg);
      } else {
        alert(res.msg);
      }
    });
  }
  render() {
    return (
      <div>
        <h3>欢迎登录</h3>
        <div>
          用户名：
          <input type="text" onChange={this.changeLogin.bind(this, "phone")} />
        </div>
        <div>
          密码：
          <input
            type="text"
            onChange={this.changeLogin.bind(this, "password")}
          />
        </div>
        <div>
          <button onClick={this.login.bind(this)}>登录</button>
        </div>
      </div>
    );
  }
}
export default Home;
